<template>
    <div id="mine">
      <!-- 刷新组件 -->
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh" success-text="刷新成功">
        <van-cell-group>
          <van-cell class="header" center>
            <template #title>
              <van-image
                width="48"
                height="48"
                :src="user.avatar"
                @click="preview"
                lazy-load
                round
              />
            </template>

            <template #label>
              <van-row type="flex" justify="center">
                <van-col span="8" style="text-align: left" offset="3">{{user.nickname}}</van-col>
              </van-row>
              <van-row type="flex" justify="center">
                <van-col span="8" style="text-align: left" offset="3">UID：{{uid()}}</van-col>
              </van-row>
              <van-row type="flex" justify="center">
                <van-col span="8" style="text-align: left" offset="3">糖果：{{user.candy}}</van-col>
              </van-row>
              <van-row type="flex" justify="center">
                <van-col span="8" style="text-align: left" offset="3">GST：{{user.m_candy}}</van-col>
              </van-row>
            </template>
          </van-cell>
        </van-cell-group>

        <!-- 菜单 -->
        <van-cell-group style="margin-top: 20px">
          <van-cell
            v-for="item in menus"
            :key="item.title"
            :title="item.title"
            @click="onClick(item.path)"
            center
            is-link
          >
            <template #icon>
              <van-image
                :src="item.icon"
                width="20"
                class="icon"
                lazy-load
              />
            </template>
          </van-cell>
        </van-cell-group>

        <!-- 退出登录 -->
        <van-cell-group style="margin-top: 20px">
          <van-cell
            title="退出登录"
            @click="logout"
            center
            is-link
          >
            <template #icon>
              <van-image
                :src="require('@/assets/logout.png')"
                width="20"
                height="20"
                class="icon"
                lazy-load
                round
              />
            </template>
          </van-cell>
        </van-cell-group>
      </van-pull-refresh>

      <!-- 菜单弹出层 -->
      <van-popup v-model="store.state.mine.showPopup" position="right" class="popup">
        <router-view/>
      </van-popup>
    </div>
</template>

<script>
  import store from '@/store'
  import axios from 'axios'
  import Vue from 'vue';
  import { ImagePreview } from 'vant';

  Vue.use(ImagePreview);

  export default {
    name: "Mine",
    data() {
      return {
        menus: [
          {icon: require('@/assets/exchange_gst.png'), title: 'GST兑换', path: '/exchange/gst'},
          {icon: require('@/assets/exchange_candy.png'), title: '糖果兑换', path: '/exchange/candy'},
          // {icon: require('@/assets/cash.png'), title: '提币'},
          // {icon: require('@/assets/security_center.png'), title: '安全中心', path: '/security_center'},
          {icon: require('@/assets/business.png'), title: '发布交易', path: '/publish_business'},
          // {icon: require('@/assets/collections_method.png'), title: '收款方式'},
          // {icon: require('@/assets/feedback.png'), title: '问题反馈', path: '/problem_feedback'},
          // {icon: require('@/assets/about_us.png'), title: '关于我们'},
        ],
        store: store,
        user: JSON.parse(sessionStorage.getItem('user')),
        isLoading: false, // 刷新中
      };
    },
    methods: {
      onClick(path) {
        store.state.mine.showPopup = true;
        this.$router.push(path);
      },
      preview() { // 头像预览
        ImagePreview({
          images: [this.user.avatar],
          showIndex: false,
        });
      },
      logout() {
        sessionStorage.removeItem("user");
        this.$router.push('/login');
      },
      uid() {
        return +this.user.uid + 20200610;
      },
      // 回退
      back() {
        store.state.mine.showPopup = false;
        this.$router.back(-1);
      },
      // 刷新
      onRefresh() {
        axios.get(store.state.api + '/getInfoByUid', {
          params: {
            uid: this.user.uid,
          }
        }).then(res => {
          if (res.data.code == '200') {
            var pattern = /^\d*(?:\.\d{0,2})?/;
            var data = res.data.data;

            this.user.candy = +data.candy.match(pattern)[0];       // 糖果
            this.user.contributionValue = data.contributionValue;  // 贡献值
            this.user.account = data.account;                      // 账号
            this.user.avatar = data.avatar;                        // 头像
            this.user.nickname = data.nickName;                    // 用户昵称
            this.user.level = data.level;                          // 等级
            this.user.activity = data.activity;                    // 活跃度
            this.user.m_candy = +data.m_candy.match(pattern)[0];   // 交易市场糖果（GST）
            this.user.walletAddress = data.walletAddress;          // 钱包地址
            this.user.token = data.token;                          // 用户token

            sessionStorage.setItem('user', JSON.stringify(this.user));
          }

          this.isLoading = false;
        }).catch(error => {
          this.isLoading = false;
        });
      }
    },
    mounted() {

    },
    updated() {

    }
  }
</script>

<style scoped>
  /* 刷新组件 */
  .van-pull-refresh {
    position: fixed;
    top: 0;
    bottom: 50px;
    width: 100%;
    overflow: auto;
  }

  .header {
    height: 200px;
    text-align: center;
    background-color: #545c80;
    /*background-image: linear-gradient(#e66465, #9198e5);*/
    /*background-color: #7232dd;*/
  }
  .icon {
    margin-right: 10px;
  }
  .popup {
    width: 100%;
    height: 100%;
    background-color: #f7f8fa;
  }
  .van-cell__label {
    color: #f7f8fa;
  }
</style>
